package com.csun.cmny.provider.service.impl;

import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCsunHumidistatThresholdMapper;
import com.csun.cmny.provider.model.constant.DeviceConstant;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Slf4j
@Service
@Transactional
public class DmcCsunHumidistatThresholdServiceImpl extends BaseService<DmcCsunHumidistatThreshold> implements DmcCsunHumidistatThresholdService {

    @Resource
    private DmcCsunHumidistatThresholdMapper dmcCsunHumidistatThresholdMapper;
    @Resource
    private DmcCsunHumidistatLocationService dmcCsunHumidistatLocationService;
    @Resource
    private DmcLifeTransducerRoomConfigService dmcLifeTransducerRoomConfigService;
    @Resource
    private DmcLifeTransducerDefaultConfigService dmcLifeTransducerDefaultConfigService;
    @Resource
    private DmcDeviceService dmcDeviceService;

    /**
     * 根据设备Id获取阈值
     *
     * @param deviceId the  device id
     * @return the dmc csun humidistat threshold
     */
    @Override
    public DmcCsunHumidistatThreshold queryThresholdByDeviceId(Long deviceId) {

        DmcCsunHumidistatThreshold result = null;

        DmcDevice dmcDevice = dmcDeviceService.selectByKey(deviceId);
        if (PublicUtil.isEmpty(dmcDevice)) {
            return null;
        }

        // 从设备配置中获取
        DmcCsunHumidistatThreshold dmcCsunHumidistatThreshold = selectByKey(deviceId);
        if (PublicUtil.isNotEmpty(dmcCsunHumidistatThreshold)) {
            return dmcCsunHumidistatThreshold;
        }

        // 判断绑定类型
        DmcCsunHumidistatLocation dmcCsunHumidistatLocation = dmcCsunHumidistatLocationService
                .selectByKey(deviceId);
        if (PublicUtil.isEmpty(dmcCsunHumidistatLocation)) {
            return null;    // 设备尚未绑定
        }

        switch (dmcCsunHumidistatLocation.getBindType()) {
            case DeviceConstant.DEVICE_BIND_TYPE_ROOM :
            case DeviceConstant.DEVICE_BIND_TYPE_LIFE :
                // 从房间配置中获取
                DmcLifeTransducerRoomConfig dmcLifeTransducerRoomConfig =
                        dmcLifeTransducerRoomConfigService.selectByKey(dmcCsunHumidistatLocation.getRoomId());
                if (PublicUtil.isEmpty(dmcLifeTransducerRoomConfig)) {
                    // 从该机构的配置中获取
                    DmcLifeTransducerDefaultConfig dmcLifeTransducerDefaultConfig =
                            dmcLifeTransducerDefaultConfigService.selectByKey(dmcDevice.getGroupId());
                    if (PublicUtil.isEmpty(dmcLifeTransducerDefaultConfig)) {
                        return null;
                    } else {
                        result = new DmcCsunHumidistatThreshold();
                        result.setDeviceId(deviceId);
                        result.setTemperatureMaximum(dmcLifeTransducerDefaultConfig.getTemperatureMaximum());
                        result.setTemperatureMinimum(dmcLifeTransducerDefaultConfig.getTemperatureMinimum());
                    }
                } else {
                    result = new DmcCsunHumidistatThreshold();
                    result.setDeviceId(deviceId);
                    result.setTemperatureMaximum(dmcLifeTransducerRoomConfig.getTemperatureMaximum());
                    result.setTemperatureMinimum(dmcLifeTransducerRoomConfig.getTemperatureMinimum());
                }
                break;
        }

        return result;
    }
}
